package com.zuma.oci.docker.transport;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

import java.net.URI;

public class DockerEngineV2Exception extends RuntimeException{

    public DockerEngineV2Exception(String host, URI uri, int statusCode, String reasonPhrase, String errors) {
        super(buildMessage(host, uri, statusCode, reasonPhrase, errors));
    }

    private static String buildMessage(String host, URI uri, int statusCode, String reasonPhrase, String errors) {
        Assert.notNull(host, "'host' must not be null");
        Assert.notNull(uri, "'uri' must not be null");
        StringBuilder message = new StringBuilder(
                "Docker API call to '" + host + uri + "' failed with status code " + statusCode);
        if (StrUtil.isNotBlank(reasonPhrase)) {
            message.append(" \"").append(reasonPhrase).append("\"");
        }
        if (StrUtil.isNotBlank(errors )) {
            message.append(" ").append(errors);
        }
        return message.toString();
    }
}
